﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Oc_MemberListItem : ObjectBase {

    protected const int RareMax = 6;
    //const float OffStarScale = 0.00117f;
    //const float OnStarScale = 0.001389f;
    protected const float StarCenter = 20.0f;
    protected const float StarGap = 27.0f;

    public MemberModel m_MemberModel;

    public UILabel m_LevelLabel;
    public UILabel m_NameLabel;
    public UILabel m_StrengtheningLabel;
    public Transform m_RareRoot_On;
    public Transform m_RareRoot_Off;

    protected Transform[] m_Rare_On;
    protected Transform[] m_Rare_Off;


    public UILabel m_LevelLabel_On;
    public UILabel m_NameLabel_On;
    public UILabel m_StrengtheningLabel_On;    
    public GameObject m_IsSelectedLabel;

    public UISprite m_OnFaceImg;
    public UISprite m_OnFaceImg_Off;

    public UISprite m_EXPBar;

   // [HideInInspector]
    protected bool m_IsOn = false;

    public GameObject m_EventRecieaver;
    public string m_OnPress;
    public string m_OnDrag;

    void Awake()
    {
       // SetOff();
    }

    public virtual void SetList(MemberModel model)
    {        
        m_MemberModel = model;

        if (m_IsSelectedLabel != null)
            m_IsSelectedLabel.SetActive(m_MemberModel.IsSelected);

        m_NameLabel.text = TextController.GetStringWithId(string.Format("charName{0}", m_MemberModel.name));
        m_LevelLabel.text = string.Format("Lv{0}", m_MemberModel.level);

        if (m_MemberModel.strengthening > 0)
            m_StrengtheningLabel.text = string.Format("+{0}", m_MemberModel.strengthening);
        else
            m_StrengtheningLabel.text = "";

      
        SetRare();

        float orizinx = 201.0f;

        m_EXPBar.SetDimensions((int)(m_MemberModel.ExpPercent() * orizinx), 11);       

        PortraitControl.SetPortrait(m_OnFaceImg_Off, m_MemberModel, false);
       
        Init_On();
    }

    protected void SetRare()
    {       
        // 처음 세팅 할때만 오브젝트 세팅
        if (m_Rare_Off == null || m_Rare_On == null)
        {
            m_Rare_On = new Transform[RareMax];
            m_Rare_Off = new Transform[RareMax];

            for (int i = 0; i < RareMax; i++)
            {
                Transform t = m_RareRoot_On.Find(string.Format("{0}", i + 1));
                m_Rare_On[i] = t;

                t = m_RareRoot_Off.Find(string.Format("{0}", i + 1));
                m_Rare_Off[i] = t;
            }
        }

        for (int i = 0; i < RareMax; i++)
        {
            m_Rare_On[i].gameObject.SetActive(m_MemberModel.rarity == i + 1);
            m_Rare_Off[i].gameObject.SetActive(m_Rare_On[i].gameObject.activeSelf);
        }
    }

    protected void Init_On()
    {
        m_NameLabel_On.text = TextController.GetStringWithId(string.Format("charName{0}", m_MemberModel.name));
        m_LevelLabel_On.text = string.Format("Lv{0}", m_MemberModel.level);

        if (m_MemberModel.strengthening > 0)
            m_StrengtheningLabel_On.text = string.Format("+{0}", m_MemberModel.strengthening);
        else
            m_StrengtheningLabel_On.text = "";

        PortraitControl.SetPortrait(m_OnFaceImg, m_MemberModel);
    }

    // 애니메이션 구동
    public void SetOn(bool isNeedSound)
    {      
        if (!m_IsOn)
        {           
            Animation t_Ani = GetComp("Animation") as Animation;           

            t_Ani.Play();
            m_IsOn = true;

            UISprite tLb = m_IsSelectedLabel.GetComponent("UISprite") as UISprite;
            Color color = tLb.color;//.a = 1f;
            color.a = 1f;
            tLb.color = color;

            color = m_StrengtheningLabel.color;
            color.a = 1f;
            m_StrengtheningLabel.color = color;

            if (m_EXPBar != null)
            {
                color = m_EXPBar.color;
                color.a = 1f;
                m_EXPBar.color = color;
            }

            if (isNeedSound)
                SoundController.PlaySound("UI_button");
        }
    }

    // on 패널 엑티브 비활성화
    public void SetOff()
    {
        if(m_IsOn)
        {
            Animation t_Ani = GetComp("Animation") as Animation;
            t_Ani.Stop();

            Transform t_OnOff = FindChild.FindInAllChilds(transform, "on");
            t_OnOff.gameObject.SetActive(false);

            t_OnOff  = FindChild.FindInAllChilds(transform, "off");
            t_OnOff.gameObject.SetActive(true);

            UISprite tLb = m_IsSelectedLabel.GetComponent("UISprite") as UISprite;
            Color color = tLb.color;
            color.a = 125/255f;
            tLb.color = color;

            color = m_StrengtheningLabel.color;
            color.a = 125/255f;
            m_StrengtheningLabel.color = color;


            if (m_EXPBar != null)
            {
                color = m_EXPBar.color;
                color.a = 125 / 255f;
                m_EXPBar.color = color;
            }

            m_IsOn = false;

            //Transform rare = FindChild.FindInAllChilds(transform, "Rare");
            //rare.localScale = new Vector3(OffStarScale, OffStarScale, 0.01f);
        }
    }

    Vector3 m_SvPrePos = Vector3.zero;
    public void OnDrag(Vector2 delta)
    {
        if (m_EventRecieaver != null && m_OnDrag != null)
            m_EventRecieaver.SendMessage(m_OnDrag, SendMessageOptions.DontRequireReceiver);

        UIDragScrollView sv = GetComp("UIDragScrollView") as UIDragScrollView;
        if(sv != null && sv.scrollView != null) // 스크롤이 가능한 상태
        {
            if(sv.scrollView.transform.localPosition != m_SvPrePos)
            {
                m_SvPrePos = sv.scrollView.transform.localPosition;

                if(!SoundController.IsPlaying(gameObject))
                {
                    SoundController.PlaySound(gameObject, "UI_button_scroll");
                }
            }
        }
    }

    public void OnPress(bool isPress)
    {
        if(isPress)
        {
             UIDragScrollView sv = GetComp("UIDragScrollView") as UIDragScrollView;
             if (sv != null && sv.scrollView != null) // 스크롤이 가능한 상태
             {
                 m_SvPrePos = sv.scrollView.transform.localPosition;
             }
        }

        if (m_EventRecieaver != null && m_OnPress != null)
            m_EventRecieaver.SendMessage(m_OnPress, SendMessageOptions.DontRequireReceiver);
    }
}
